﻿using System.Threading.Tasks;
using Microsoft.AspNetCore.SignalR;
using Microsoft.EntityFrameworkCore.Internal;
using NetCoreSignalRChat.Common;

namespace NetCoreSignalRChat.Hubs
{
    public class ChatHub : Hub
    {
        public async Task SendMessage(string user, string message)
        {
            var connectIds = SignalRHelper.GetUserConnectionIds(user);
            if (connectIds.Any())
                await Clients.Clients(connectIds).SendAsync("ReceiveMessage", user, message);
            else
                // 调用所有客户端的ReceiveMessage方法
                await Clients.AllExcept(Context.ConnectionId).SendAsync("ReceiveMessage", user, message);
        }

        public bool Addtoken(string token)
        {
            SignalRHelper.AddConnectionId(token, Context.ConnectionId);

            return true;
        }
    }
}
